package com.botao.tip.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.botao.tip.entity.Finance;
import com.botao.tip.service.FinanceService;
import com.botao.tip.service.FinanceTotalService;
import com.botao.tip.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : vip865047755@126.com
 * @version :JDK1.8
 * @date : 2020-12-04 20:03
 * @File : FinanceController.java
 * @software: IntelliJ IDEA
 */
@RestController
@RequestMapping("/api/finance/")
public class FinanceController {
    @Autowired
    private FinanceService financeService;

    @Autowired
    private FinanceTotalService financeTotalService;

    @GetMapping("get/all")
    public AjaxResult getAll(@RequestParam int currentPage, @RequestParam int pageSize) {
        Page<Finance> financePage = new Page<>(currentPage, pageSize);
        Page<Finance> page = financeService.page(financePage);
        List<Finance> records = page.getRecords();
        long total = page.getTotal();
        long current = page.getCurrent();
        long size = page.getSize();
        boolean hasNext = page.hasNext();
        boolean hasPrevious = page.hasPrevious();

        HashMap<Object, Object> map = new HashMap<>();
        map.put("records", records);
        map.put("total", total);
        map.put("current", current);
        map.put("size", size);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return AjaxResult.success(map);
    }

    @PostMapping("insert")
    public AjaxResult insert(Finance finance) {
        System.out.println(finance);
        boolean insert = financeService.insert(finance);
        return insert ? AjaxResult.success() : AjaxResult.error();
    }


    //数据分析

    @GetMapping("hadoop")
    public AjaxResult hadoop() {
        //资金亏欠
        int moneySum = financeTotalService.getMoneySum();
        HashMap<Object, Object> map = new HashMap<>();
        map.put("money", moneySum);
        //收入
        Map<String, Integer> ioMap = financeService.inOuts();
        Integer in = ioMap.get("in");
        Integer out = ioMap.get("out");
        map.put("in", in);
        map.put("out", out);
        return AjaxResult.success(map);
    }

}
